package com.leetcode;

import ds.ListNode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No160
 * @description
 * @date 2021/1/28 下午4:07
 */
public class No160 {
    public static void main(String[] args) {
        ListNode a1 = new ListNode(11);
        ListNode a2 = new ListNode(12);
        ListNode a3 = new ListNode(13);
        ListNode b1 = new ListNode(21);
        ListNode b2 = new ListNode(21);
        ListNode c1 = new ListNode(31);
        ListNode c2 = new ListNode(32);
        ListNode c3 = new ListNode(33);
        ListNode c4 = new ListNode(34);
        a1.next = a2;
        a2.next = a3;
        a3.next = c1;
        b1.next = b2;
        b2.next = c1;
        c1.next = c2;
        c2.next = c3;
        c3.next = c4;

        ListNode res = getIntersectionNode(a1,b1);
        System.out.println(res.val);
    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode l1 = headA,l2 = headB;
        while (l1 != l2){
            l1 = (l1 == null) ? headB : l1.next;
            l2 = (l2 == null) ? headA : l2.next;
        }
        return l1;
    }

}
